package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void addBed(BedDto bedDto) {
        Bed bean = BeanUtil.toBean(bedDto, Bed.class);
        bean.setCreateTime(LocalDateTime.now());
        bean.setUpdateTime(LocalDateTime.now());
        bean.setBedStatus(1);
        bean.setCreateBy(1L);
        bean.setUpdateBy(1L);
        //log.info("添加的床位信息:{}",bean);
        try {
            bedMapper.insertBed(bean);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public void deleteBedByBedId(Long id) {
        bedMapper.deleteBedByBedId(id);
    }

    @Override
    public BedVo readBedByBedId(Long id) {
        return bedMapper.readBedByBedId(id);
    }

    @Override
    public void updateBed(BedDto bedDto) {
        //log.info("转换状态前的信息:{}",bedDto);
        //bedDto接不到状态的值
        // BedVo bedVo=readBedByBedId(bedDto.getId());
        //log.info("最初回显的数据，未被修改过:{}",bedVo);
        //bedNumber=111, bedStatus=0, roomId=77, sort=1,
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        //log.info("需要修改的信息:{}",bed);
        bed.setUpdateTime(LocalDateTime.now());
        // bed.setBedStatus(bedVo.getBedStatus());
        bed.setUpdateBy(1L);
        bedMapper.updateBed(bed);
        //想复杂了，反正用户也只能改改排序和名字，其他又改不了，接不接无所谓
    }

}

